package com.pureut.storage.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pureut.storage.domain.MaterialChange;
import com.pureut.storage.domain.WmsStockInfo;
import com.pureut.storage.domain.order.domain.CustomerManagement;
import com.pureut.storage.domain.order.domain.OrderSupplierManagement;
import com.pureut.storage.dto.MaterialChangeDto;
import com.pureut.storage.dto.WmsStockInfoDto;
import com.pureut.storage.mapper.CustomerManagementMapper;
import com.pureut.storage.mapper.MaterialChangeMapper;
import com.pureut.storage.mapper.OrderSupplierManagementMapper;
import com.pureut.storage.mapper.WmsStockInfoMapper;
import com.pureut.storage.service.MaterialChangeService;
import com.pureut.storage.vo.MaterialChangeVo;
import com.sale.common.core.exception.GlobalException;
import com.sale.common.core.utils.bean.BeanUtils;
import com.sale.common.mybatis.core.page.PageQuery;
import com.sale.common.mybatis.core.page.TableDataInfo;
import com.sale.common.satoken.utils.LoginHelper;
import com.sale.system.api.RemoteSysCustomerService;
import com.sale.system.api.RemoteSysSupplierService;
import com.sale.system.api.RemoteUnitService;
import com.sale.system.api.model.MaterialUnitDto;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author: CM
 * @date: 2023/1/9  16:41
 */
@Service
public class MaterialChangeServiceImpl extends ServiceImpl<MaterialChangeMapper, MaterialChange> implements MaterialChangeService {

    @Resource
    private WmsStockInfoMapper wmsStockInfoMapper;

    @Resource
    private MaterialChangeMapper materialChangeMapper;

    @Resource
    private CustomerManagementMapper customerManagementMapper;

    @Resource
    private OrderSupplierManagementMapper orderSupplierManagementMapper;

    @DubboReference
    RemoteSysCustomerService remoteSysCustomerService;

    @DubboReference
    RemoteSysSupplierService remoteSysSupplierService;

    @DubboReference
    RemoteUnitService remoteUnitService;

    /**
     * 查询产品号码对应的信息
     * @param number
     * @return
     */
    @Override
    public WmsStockInfoDto viewStock(String number) {
        //单位集合
        Map<String, MaterialUnitDto> unitByCodeToMap = remoteUnitService.getUnitByCodeToMap();

        WmsStockInfo wmsStockInfo = wmsStockInfoMapper.selectOne(new QueryWrapper<WmsStockInfo>().lambda().eq(WmsStockInfo::getWsiMaterialNumber, number));
        if(wmsStockInfo == null){
            throw new GlobalException("未查询到该物料，请核实!");
        }
        WmsStockInfoDto wmsStockInfoDto=new WmsStockInfoDto();
        BeanUtils.copyProperties(wmsStockInfo,wmsStockInfoDto);
        //查询供应商
        Map<String, String> openSupplierFinance = remoteSysSupplierService.getOpenSupplierFinance();
//        OrderSupplierManagement orderSupplierManagement = orderSupplierManagementMapper.selectOne(new QueryWrapper<OrderSupplierManagement>()
//            .lambda().eq(OrderSupplierManagement::getSupplierNum, ));

        if (wmsStockInfo.getWsiSupplierCode() != null){
            wmsStockInfoDto.setSupplierName(openSupplierFinance.get(wmsStockInfo.getWsiSupplierCode()));
        }
        //查询客户信息
//        CustomerManagement customerManagement = customerManagementMapper.selectOne(new QueryWrapper<CustomerManagement>()
//            .lambda().eq(CustomerManagement::getCustomerNum, wmsStockInfo.getWsiCustomCode()));
        Map<String, String> customerFinance = remoteSysCustomerService.getOpenCustomerFinance();
        if (wmsStockInfo.getWsiCustomCode() !=null){
            wmsStockInfoDto.setCustomerName(customerFinance.get(wmsStockInfo.getWsiCustomCode()));
        }

        //单位
        if(wmsStockInfo.getMaterialUnit() != null){
            wmsStockInfoDto.setMaterialUnit(String.valueOf(wmsStockInfo.getMaterialUnit()));
            wmsStockInfoDto.setMaterialUnitDict(unitByCodeToMap.get(String.valueOf(wmsStockInfo.getMaterialUnit())).getUnitName());
        }

        return wmsStockInfoDto;


    }

    /**
     * 物料变更新增
     * @param materialChangeVo
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean addChange(MaterialChangeVo materialChangeVo) {

        //查询出原有的物料属性
        WmsStockInfo wmsStockInfo = wmsStockInfoMapper.selectOne(new QueryWrapper<WmsStockInfo>().lambda().eq(WmsStockInfo::getWsiMaterialNumber, materialChangeVo.getMaterialNumber()));

        //修改物料属性
        WmsStockInfo wmsStockInfo1=new WmsStockInfo();
        //设置id
        wmsStockInfo1.setId(wmsStockInfo.getId());
        //设置幅宽
        wmsStockInfo1.setWsiWidth(materialChangeVo.getBreadth());
        //设置长度
        wmsStockInfo1.setWsiLength(materialChangeVo.getLength());
        //设置卷径
        wmsStockInfo1.setWsiDiameter(materialChangeVo.getRollingDiameter());
        //设置毛重
        if (materialChangeVo.getGrossWeight()!=0){
            wmsStockInfo1.setWsiGorssWeight(materialChangeVo.getGrossWeight());
        }

        //设置净重
        if (materialChangeVo.getNetWeight()!=0){
            wmsStockInfo1.setWsiNetWeight(materialChangeVo.getNetWeight());
        }

        //设置克重
        wmsStockInfo1.setWsiGrawWeight(materialChangeVo.getGramWeight());
        //设置厚度
        wmsStockInfo1.setWsiThick(materialChangeVo.getThick());
        //设置接头
        wmsStockInfo1.setWsiJoint(materialChangeVo.getJoint());
        //设置生产批次
        wmsStockInfo1.setWsiMaterialBatch(materialChangeVo.getBatch());

        int i = wmsStockInfoMapper.updateById(wmsStockInfo1);

        if (i>0) {
            //查询修改之后的数据
            WmsStockInfo updateWmsStockInfo = wmsStockInfoMapper.selectOne(new QueryWrapper<WmsStockInfo>().lambda().eq(WmsStockInfo::getWsiMaterialNumber, materialChangeVo.getMaterialNumber()));


            //如果修改了批号

            if (materialChangeVo.getBatch()!=null) {

                if (!materialChangeVo.getBatch().equals(wmsStockInfo.getWsiMaterialBatch())) {
                    MaterialChange materialChange = new MaterialChange();
                    materialChange.setMaterialNumber(materialChangeVo.getMaterialNumber());
                    materialChange.setMaterialCode(updateWmsStockInfo.getWsiMaterialCode());
                    materialChange.setChangeUser(LoginHelper.getUsername());
                    materialChange.setChangeTime(new Date());

                    materialChange.setMaterialNumber(materialChangeVo.getMaterialNumber());
                    materialChange.setMaterialCode(updateWmsStockInfo.getWsiMaterialCode());
                    materialChange.setChangeUser(LoginHelper.getUsername());
                    materialChange.setChangeTime(new Date());
                    //xiu
                    materialChange.setChangeType("生产批次");
                    //xiu
                    materialChange.setOriginalValue(wmsStockInfo.getWsiMaterialBatch());
                    //xiu
                    materialChange.setExistingValue(updateWmsStockInfo.getWsiMaterialBatch());

                    materialChange.setDeptId(LoginHelper.getDeptId());
                    boolean save = save(materialChange);
                    if (!save) {
                        throw new GlobalException("变更失败");
                    }
                }
            }

            //如果修改了幅宽
            if (materialChangeVo.getBreadth()!=null) {
                if (!materialChangeVo.getBreadth().equals(wmsStockInfo.getWsiWidth())) {
                    MaterialChange materialChange = new MaterialChange();
                    materialChange.setMaterialNumber(materialChangeVo.getMaterialNumber());
                    materialChange.setMaterialCode(updateWmsStockInfo.getWsiMaterialCode());
                    materialChange.setChangeUser(LoginHelper.getUsername());
                    materialChange.setChangeTime(new Date());

                    //xiu
                    materialChange.setChangeType("幅宽(mm)");
                    //xiu
                    materialChange.setOriginalValue(wmsStockInfo.getWsiWidth());
                    //xiu
                    materialChange.setExistingValue(updateWmsStockInfo.getWsiWidth());
                    materialChange.setDeptId(LoginHelper.getDeptId());
                    boolean save = save(materialChange);
                    if (!save) {
                        throw new GlobalException("变更失败");
                    }
                }
            }

            //如果修改了长度
            if (materialChangeVo.getLength()!=null) {
                if (!materialChangeVo.getLength().equals(wmsStockInfo.getWsiLength())) {
                    MaterialChange materialChange = new MaterialChange();
                    materialChange.setMaterialNumber(materialChangeVo.getMaterialNumber());
                    materialChange.setMaterialCode(updateWmsStockInfo.getWsiMaterialCode());
                    materialChange.setChangeUser(LoginHelper.getUsername());
                    materialChange.setChangeTime(new Date());
                    //xiu
                    materialChange.setChangeType("长度");
                    //xiu
                    materialChange.setOriginalValue(wmsStockInfo.getWsiLength());
                    //xiu
                    materialChange.setExistingValue(updateWmsStockInfo.getWsiLength());
                    materialChange.setDeptId(LoginHelper.getDeptId());
                    boolean save = save(materialChange);
                    if (!save) {
                        throw new GlobalException("变更失败");
                    }
                }
            }

            //如果修改了卷径
            if (materialChangeVo.getRollingDiameter()!=null){
            if (!materialChangeVo.getRollingDiameter().equals(wmsStockInfo.getWsiDiameter())) {
                MaterialChange materialChange = new MaterialChange();
                materialChange.setMaterialNumber(materialChangeVo.getMaterialNumber());
                materialChange.setMaterialCode(updateWmsStockInfo.getWsiMaterialCode());
                materialChange.setChangeUser(LoginHelper.getUsername());
                materialChange.setChangeTime(new Date());
                //xiu
                materialChange.setChangeType("卷径");
                //xiu
                materialChange.setOriginalValue(wmsStockInfo.getWsiDiameter());
                //xiu
                materialChange.setExistingValue(updateWmsStockInfo.getWsiDiameter());
                materialChange.setDeptId(LoginHelper.getDeptId());

                boolean save = save(materialChange);
                if (!save){
                    throw new GlobalException("变更失败");
                }
            }
            }

            Double grossWeight = materialChangeVo.getGrossWeight();
            Double wsiGorssWeight = wmsStockInfo.getWsiGorssWeight();
            int i1 = grossWeight.compareTo(wsiGorssWeight);


            //如果修改了毛重
            if (i1!=0) {
                MaterialChange materialChange = new MaterialChange();
                materialChange.setMaterialNumber(materialChangeVo.getMaterialNumber());
                materialChange.setMaterialCode(updateWmsStockInfo.getWsiMaterialCode());
                materialChange.setChangeUser(LoginHelper.getUsername());
                materialChange.setChangeTime(new Date());
                //xiu
                materialChange.setChangeType("毛重");
                //xiu
                materialChange.setOriginalValue(wmsStockInfo.getWsiGorssWeight().toString());
                //xiu
                materialChange.setExistingValue(updateWmsStockInfo.getWsiGorssWeight().toString());
                materialChange.setDeptId(LoginHelper.getDeptId());

                boolean save = save(materialChange);
                if (!save){
                    throw new GlobalException("变更失败");
                }
            }

            Double grossWeight1 = materialChangeVo.getNetWeight();
            Double wsiGorssWeight1 = wmsStockInfo.getWsiNetWeight();
            int i2 = grossWeight1.compareTo(wsiGorssWeight1);
            //如果修改了净重
            if (i2!=0) {
                MaterialChange materialChange = new MaterialChange();
                materialChange.setMaterialNumber(materialChangeVo.getMaterialNumber());
                materialChange.setMaterialCode(updateWmsStockInfo.getWsiMaterialCode());
                materialChange.setChangeUser(LoginHelper.getUsername());
                materialChange.setChangeTime(new Date());
                //xiu
                materialChange.setChangeType("净重");
                //xiu
                materialChange.setOriginalValue(wmsStockInfo.getWsiNetWeight().toString());
                //xiu
                materialChange.setExistingValue(updateWmsStockInfo.getWsiNetWeight().toString());

                materialChange.setDeptId(LoginHelper.getDeptId());

                boolean save = save(materialChange);
                if (!save){
                    throw new GlobalException("变更失败");
                }
            }

            //如果修改了克重
            if (materialChangeVo.getGramWeight()!=null) {
                if (!materialChangeVo.getGramWeight().equals(wmsStockInfo.getWsiGrawWeight())) {
                    MaterialChange materialChange = new MaterialChange();
                    materialChange.setMaterialNumber(materialChangeVo.getMaterialNumber());
                    materialChange.setMaterialCode(updateWmsStockInfo.getWsiMaterialCode());
                    materialChange.setChangeUser(LoginHelper.getUsername());
                    materialChange.setChangeTime(new Date());
                    //xiu
                    materialChange.setChangeType("克重");
                    //xiu
                    materialChange.setOriginalValue(wmsStockInfo.getWsiGrawWeight());
                    //xiu
                    materialChange.setExistingValue(updateWmsStockInfo.getWsiGrawWeight());

                    materialChange.setDeptId(LoginHelper.getDeptId());

                    boolean save = save(materialChange);
                    if (!save) {
                        throw new GlobalException("变更失败");
                    }
                }
            }

            //如果修改了厚度
            if (materialChangeVo.getThick()!=null) {
                if (!materialChangeVo.getThick().equals(wmsStockInfo.getWsiThick())) {
                    MaterialChange materialChange = new MaterialChange();
                    materialChange.setMaterialNumber(materialChangeVo.getMaterialNumber());
                    materialChange.setMaterialCode(updateWmsStockInfo.getWsiMaterialCode());
                    materialChange.setChangeUser(LoginHelper.getUsername());
                    materialChange.setChangeTime(new Date());
                    //xiu
                    materialChange.setChangeType("厚度");
                    //xiu
                    materialChange.setOriginalValue(wmsStockInfo.getWsiThick().toString());
                    //xiu
                    materialChange.setExistingValue(updateWmsStockInfo.getWsiThick().toString());

                    materialChange.setDeptId(LoginHelper.getDeptId());

                    boolean save = save(materialChange);
                    if (!save) {
                        throw new GlobalException("变更失败");
                    }
                }
            }

            //如果修改了接头
            if (materialChangeVo.getJoint()!=null) {
                if (!materialChangeVo.getJoint().equals(wmsStockInfo.getWsiJoint())) {
                    MaterialChange materialChange = new MaterialChange();
                    materialChange.setMaterialNumber(materialChangeVo.getMaterialNumber());
                    materialChange.setMaterialCode(updateWmsStockInfo.getWsiMaterialCode());
                    materialChange.setChangeUser(LoginHelper.getUsername());
                    materialChange.setChangeTime(new Date());
                    //xiu
                    materialChange.setChangeType("接头");
                    //xiu
                    materialChange.setOriginalValue(wmsStockInfo.getWsiThick().toString());
                    //xiu
                    materialChange.setExistingValue(updateWmsStockInfo.getWsiThick().toString());

                    materialChange.setDeptId(LoginHelper.getDeptId());

                    boolean save = save(materialChange);
                    if (!save) {
                        throw new GlobalException("变更失败");
                    }
                }
            }
        }else {
            throw new GlobalException("变更失败");
        }

       return i>0;
    }

    /**
     * 物料变更列表查询
     * @param materialChangeVo
     * @return
     */
    @Override
    public TableDataInfo<MaterialChangeDto> getList(MaterialChangeVo materialChangeVo, PageQuery pageQuery) {
        Page<MaterialChangeDto> materialChangeList = materialChangeMapper.getMaterialChangeList(materialChangeVo, pageQuery.build());
        return TableDataInfo.build(materialChangeList);

    }


}
